#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, M = 1e6 + 10;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timetamp;
int sta[N], top;
int in_sta[N];
int dout[N];
int id[N], ssc_cnt, Size[N];
vector<int> g[N];
int vis[N];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void tarjan(int u) {
    dfn[u] = low[u] = ++timetamp;
    sta[++top] = u;
    in_sta[u] = 1;
    for (int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if (!dfn[j]) {
            tarjan(j);
            low[u] = min(low[u], low[j]);
        } else if (in_sta[j]) {
            low[u] = min(low[u], dfn[j]);
        }
    }

    if (dfn[u] == low[u]) {
        ++ssc_cnt;
        int y;
        do {
            y = sta[top--];
            in_sta[y] = 0;
            id[y] = ssc_cnt;
            Size[ssc_cnt]++;
        } while (y != u);
    }
}

void bfs() {
    queue<int> q;
    for (int i = 1; i <= ssc_cnt; i++) {
        if (Size[i] >= 2) {
            q.push(i);
        }
    }

    while (q.size()) {
        auto t = q.front();
        q.pop();
        if (vis[t]) continue;
        vis[t] = 1;
        for (auto v : g[t]) {
            q.push(v);
        }
    }
}
int main() {
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof(h));
    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
    }
    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) {
            tarjan(i);
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = h[i]; ~j; j = ne[j]) {
            int v = e[j];
            if (id[i] != id[v]) {
                g[id[v]].push_back(id[i]);
            }
        }
    }

    
    bfs();

    int res = 0;
    for (int i = 1; i <= ssc_cnt; i++) {
        if (vis[i] == 1) {
            res += Size[i];
        }
    }


    cout << res << endl;

    return 0;
}